﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YJKRevitKernel.ElementSearchUtility.LinkDocumentSuite;
using YJKGeometryFuncs;
using YJKPatternSign.StairsRoomSection.Kit;

namespace YJKPatternSign.StairsRoomSection.DataDefine
{
	public class prepareElementsCls
	{
		public List<BaseLinkDocumentResultBean> LstDoor
		{
			get
			{
				return this.m_lstDoor;
			}
		}

		public List<BaseLinkDocumentResultBean> LstFloor
		{
			get
			{
				return this.m_lstFloor;
			}
		}

		public List<BaseLinkDocumentResultBean> LstGrid
		{
			get
			{
				return this.m_lstGrid;
			}
		}

		public List<BaseLinkDocumentResultBean> LstLevel
		{
			get
			{
				return this.m_lstLevel;
			}
		}

		public List<BaseLinkDocumentResultBean> LstStairs
		{
			get
			{
				return this.m_lstStairs;
			}
		}

		public List<BaseLinkDocumentResultBean> LstWindow
		{
			get
			{
				return this.m_lstWindow;
			}
		}

		public List<BaseLinkDocumentResultBean> LstRailing
		{
			get
			{
				return this.m_lstRailing;
			}
		}

		public List<BaseLinkDocumentResultBean> LstWall
		{
			get
			{
				return this.m_lstWall;
			}
			set
			{
				this.m_lstWall = value;
			}
		}

		public List<BaseLinkDocumentResultBean> LstColumn
		{
			get
			{
				return this.m_lstColumn;
			}
			set
			{
				this.m_lstColumn = value;
			}
		}

		public prepareElementsCls(SectionViewData SecViewData, int iFlg = 1)
		{
			this.m_ViewData = SecViewData;
			this.m_doc = SecViewData.SectionView.Document;
			this.m_iFlg = iFlg;
			Outline outline = new Outline(SecViewData.MinPt, SecViewData.MaxPt);
			this.m_intersectsFilter = new BoundingBoxIntersectsFilter(outline);
		}

		public bool InitAllElement()
		{
			this.InitWalls();
			this.InitFrames();
			this.InitFloor();
			this.InitColumns();
			if (this.m_iFlg == 1)
			{
				this.InitStairs();
				this.InitStairsRailing();
				this.InitDoors();
				this.InitWindows();
				this.InitLevel();
				this.InitGird();
				return true;
			}
			if (this.m_iFlg == 2)
			{
				this.InitStairs();
				return true;
			}
			if (this.m_iFlg == 3)
			{
				this.InitRoofs();
				return true;
			}
			return false;
		}

		public List<BaseLinkDocumentResultBean> GetAllFillElement()
		{
			List<BaseLinkDocumentResultBean> list = new List<BaseLinkDocumentResultBean>();
			list.AddRange(this.m_lstColumn);
			list.AddRange(this.m_lstFloor);
			list.AddRange(this.m_lstFrame);
			list.AddRange(this.m_lstRoof);
			list.AddRange(this.m_lstStairs);
			list.AddRange(this.m_lstWall);
			return list;
		}

		public List<RevitLinkInstance> GetAllLinkInstance()
		{
			List<RevitLinkInstance> list = new List<RevitLinkInstance>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(RevitLinkInstance));
			foreach (Element element in filteredElementCollector.WherePasses(elementClassFilter).WhereElementIsNotElementType().ToElements())
			{
				list.Add(element as RevitLinkInstance);
			}
			return list;
		}

		public void InitColumns()
		{
			ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns);
			this.m_lstColumn.AddRange(this.FilterElemByViewAndKind(filter, true));
		}

		public void InitFloor()
		{
			ElementClassFilter filter = new ElementClassFilter(typeof(Floor));
			this.m_lstFloor.AddRange(this.FilterElemByViewAndKind(filter, true));
		}

		public void InitFrames()
		{
			ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming);
			this.m_lstFrame.AddRange(this.FilterElemByViewAndKind(filter, true));
		}

		public void InitWalls()
		{
			ElementClassFilter filter = new ElementClassFilter(typeof(Wall));
			this.m_lstWall.AddRange(this.FilterElemByViewAndKind(filter, true));
		}

		public void InitGird()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Grid));
			IList<Element> list = filteredElementCollector.WherePasses(elementClassFilter).WhereElementIsNotElementType().ToElements();
			if (list.Count == 0)
			{
				using (List<BaseLinkDocumentResultBean>.Enumerator enumerator = new FilteredElementCollectorLinkDocument(this.m_doc).WherePass(elementClassFilter).ToBeans().GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						BaseLinkDocumentResultBean item = enumerator.Current;
						this.m_lstGrid.Add(item);
					}
					return;
				}
			}
			foreach (Element element in list)
			{
				if ((element as Grid).CanBeVisibleInView(this.m_ViewData.SectionView))
				{
					BaseLinkDocumentResultBean item2 = new BaseLinkDocumentResultBean(element, null, null);
					this.m_lstGrid.Add(item2);
				}
			}
		}

		public void InitLevel()
		{
			ElementClassFilter filter = new ElementClassFilter(typeof(Level));
			using (List<BaseLinkDocumentResultBean>.Enumerator enumerator = new FilteredElementCollectorLinkDocument(this.m_doc).WherePass(filter).ToBeans().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					BaseLinkDocumentResultBean bean = enumerator.Current;
					if (this.m_lstLevel.FindIndex((BaseLinkDocumentResultBean e) => Geometry.IsEqual((e.UseElement as Level).Elevation, (bean.UseElement as Level).Elevation)) == -1)
					{
						this.m_lstLevel.Add(bean);
					}
				}
			}
		}

		public void InitRoofs()
		{
			ElementClassFilter filter = new ElementClassFilter(typeof(RoofBase));
			this.m_lstRoof.AddRange(this.FilterElemByViewAndKind(filter, true));
		}

		public void InitStairs()
		{
			ElementClassFilter filter = new ElementClassFilter(typeof(Stairs));
			this.m_lstStairs.AddRange(this.FilterElemByViewAndKind(filter, true));
		}

		public void InitStairsRailing()
		{
			FilteredElementCollectorLinkDocument filteredElementCollectorLinkDocument = new FilteredElementCollectorLinkDocument(this.m_doc);
			filteredElementCollectorLinkDocument.AddViewLimit(this.m_intersectsFilter);
			filteredElementCollectorLinkDocument.OfCalss(typeof(Railing));
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in filteredElementCollectorLinkDocument.ToBeans())
			{
				if (baseLinkDocumentResultBean.LstParentRevitLinkInstance.Count == 0)
				{
					this.m_lstRailing.Add(baseLinkDocumentResultBean);
				}
			}
		}

		public void InitWindows()
		{
			FilteredElementCollectorLinkDocument filteredElementCollectorLinkDocument = new FilteredElementCollectorLinkDocument(this.m_doc);
			filteredElementCollectorLinkDocument.AddViewLimit(this.m_intersectsFilter);
			filteredElementCollectorLinkDocument.OfCategory(BuiltInCategory.OST_Windows);
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in filteredElementCollectorLinkDocument.ToBeans())
			{
				if (baseLinkDocumentResultBean.LstParentRevitLinkInstance.Count == 0)
				{
					this.m_lstWindow.Add(baseLinkDocumentResultBean);
				}
			}
		}

		public void InitDoors()
		{
			FilteredElementCollectorLinkDocument filteredElementCollectorLinkDocument = new FilteredElementCollectorLinkDocument(this.m_doc);
			filteredElementCollectorLinkDocument.AddViewLimit(this.m_intersectsFilter);
			filteredElementCollectorLinkDocument.OfCategory(BuiltInCategory.OST_Doors);
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in filteredElementCollectorLinkDocument.ToBeans())
			{
				if (baseLinkDocumentResultBean.LstParentRevitLinkInstance.Count == 0)
				{
					this.m_lstDoor.Add(baseLinkDocumentResultBean);
				}
			}
		}

		private bool isElementThroughLine(BaseLinkDocumentResultBean linkElement)
		{
			Curve secLine = this.m_ViewData.SecLine;
			Transform totalTransform = linkElement.GetTotalTransform();
			Line line = secLine.CreateTransformed(totalTransform.Inverse) as Line;
			XYZ xyz = this.m_ViewData.m_viewDir;
			xyz = totalTransform.Inverse.OfVector(xyz);
			return CommonFun.SureElementInPlanLine(linkElement.UseElement, line, this.m_ViewData.m_viewDir);
		}

		public List<BaseLinkDocumentResultBean> FilterElemByViewAndKind(ElementFilter filter, bool bIsThroughLine = true)
		{
			FilteredElementCollectorLinkDocument filteredElementCollectorLinkDocument = new FilteredElementCollectorLinkDocument(this.m_doc);
			filteredElementCollectorLinkDocument.AddViewLimit(this.m_intersectsFilter);
			filteredElementCollectorLinkDocument.WherePass(filter);
			List<BaseLinkDocumentResultBean> list = filteredElementCollectorLinkDocument.ToBeans();
			List<BaseLinkDocumentResultBean> list2 = new List<BaseLinkDocumentResultBean>();
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in list)
			{
				if (bIsThroughLine)
				{
					if (this.isElementThroughLine(baseLinkDocumentResultBean) && baseLinkDocumentResultBean.LstParentRevitLinkInstance.Count == 0)
					{
						list2.Add(baseLinkDocumentResultBean);
					}
				}
				else if (!this.isElementThroughLine(baseLinkDocumentResultBean) && baseLinkDocumentResultBean.LstParentRevitLinkInstance.Count == 0)
				{
					list2.Add(baseLinkDocumentResultBean);
				}
			}
			return list2;
		}

		private int m_iFlg = 1;

		private Document m_doc;

		private List<BaseLinkDocumentResultBean> m_lstColumn = new List<BaseLinkDocumentResultBean>();

		private List<BaseLinkDocumentResultBean> m_lstDoor = new List<BaseLinkDocumentResultBean>();

		private List<BaseLinkDocumentResultBean> m_lstFloor = new List<BaseLinkDocumentResultBean>();

		private List<BaseLinkDocumentResultBean> m_lstFrame = new List<BaseLinkDocumentResultBean>();

		private List<BaseLinkDocumentResultBean> m_lstGrid = new List<BaseLinkDocumentResultBean>();

		private List<BaseLinkDocumentResultBean> m_lstLevel = new List<BaseLinkDocumentResultBean>();

		private List<BaseLinkDocumentResultBean> m_lstRoof = new List<BaseLinkDocumentResultBean>();

		private List<BaseLinkDocumentResultBean> m_lstStairs = new List<BaseLinkDocumentResultBean>();

		private List<BaseLinkDocumentResultBean> m_lstWall = new List<BaseLinkDocumentResultBean>();

		private List<BaseLinkDocumentResultBean> m_lstWindow = new List<BaseLinkDocumentResultBean>();

		private List<BaseLinkDocumentResultBean> m_lstRailing = new List<BaseLinkDocumentResultBean>();

		private SectionViewData m_ViewData;

		private BoundingBoxIntersectsFilter m_intersectsFilter;
	}
}
